Predefined Entities

What if you wanted an element that contained the following data?

“A” is greater than “B” (A > B) and “B” is greater than “C” (B > C).  It is then true that “A” is greater than “C” (A > C).

Any XML processor would have a  problem with this statement.  

<truism>
“A” is greater than “B” (A > B) and “B” is greater than “C” (B > C).  It is then true that “A” is greater than “C” (A > C).
</truism>

The reason is its use of markup characters in non-markup code.  The greater than (>) and  equal (=) symbols are used within element data. When the XML processor encounters this markup data, it does not know how to process it and produces fatal errors.  To allow for markup inside of non-markup code, we use predefined entities.  

Predefined entities are substitutions for markup characters that you can put within your non-markup code.  When the XML encounters these entities, it simply substitutes the correct character in your data.  The following table provides a list of predefined entities for common markup characters.  To add one of the characters to an element, simply substitute the replacement text for the actual character in your document.

Symbol	Replacement
&	&amp;
>	&gt;
<	&lt;
’	&apos;
“	&quot;

Using our previous example, let’s change our element so that it uses the appropriate predefined entities.

<?xml version=”1.0”?>
<truism>
&quot;A&quot; is greater than &quot;B&quot; (A &gt; B) and &quot;B&quot; is greater than &quot;C&quot; (B &gt; C).  It is then true that &quot;A&quot; is greater than &quot;C&quot; (A &gt; C).
</truism>

File: Chapter 3\PredefinedEntities.xml

File: Chapter 3\PredefinedEntities.xml in IE5

Alternately, you can enter the hexadecimal value for a given character code.  This is done by using the &# markup characters.  As an example, let’s alter the previous table using hexadecimal values instead of their character references.  Note that there are some that are doubled-up.  We will explain this later.

Symbol	Replacement
&	&#38;#38;
>	&#62;
<	&#38;#60;
‘	&#39;
“	&#34;

If we used the hexadecimal values instead of the predefined entity character values, our XML would look like the following:

<?xml version="1.0"?>
<truism>
&#34;A&#34; is greater than &#34;B&#34; (A &#62; B) and &#34;B&#34; is greater than &#34;C&#34; (B &#62; C).  It is then true that &#34;A&#34; is greater than &#34;C&#34; (A &#62; C).
</truism>
File: Chapter 3\HexValues.xml